package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddCache;
import com.baizhi.annotation.AddLog;
import com.baizhi.annotation.DelCache;
import com.baizhi.dao.AdminDAO;
import com.baizhi.dao.AdminMapper;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    private static final Logger log = LoggerFactory.getLogger(AdminServiceImpl.class);
    @Resource
   AdminDAO adminDAO;
   @Resource
   HttpServletRequest request;
    @Override
    public HashMap<String, Object> login(Admin admin, String enCode) {

        //获取验证码
        String code = (String) request.getServletContext().getAttribute("code");
        log.info("用户输入的验证码:{}",code);
        log.info("原本的验证码:{}",enCode);
        HashMap<String, Object> map = new HashMap<>();
        //判断验证码是否一致
        if(code.equals(enCode)){
            //判断用户是否存在
            Admin admins = adminDAO.queryByUsername(admin.getUsername());
            if (admins!=null) {
                //判断用户是否正常
                if(admins.getStatus().equals("1")){
                    //存储用户信息
                    request.getServletContext().setAttribute("admin",admins);
                    //判断密码是否正确
                    if(admin.getPassword().equals(admins.getPassword())){
                        map.put("message",admins);
                        map.put("status",200);
                    }else{
                        map.put("message","密码不正确");
                        map.put("status",400);
                    }

                }else{
                    map.put("message","用户已被冻结");
                    map.put("status",400);
                }

            }else{
                map.put("message","用户不存在");
                map.put("status",400);
            }

        }else{
            map.put("message","验证码不正确");
            map.put("status",400);
        }

        return map;
    }
    @AddCache
    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer pageSize) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        int cont = adminDAO.queryCont();
        map.put("total",cont);
        List<Admin> adminList = adminDAO.queryAllPage((page - 1) * pageSize,pageSize);
        map.put("rows",adminList);
        return map;
    }
    @DelCache
    @AddLog(value = "删除管理员")
    @Override
    public HashMap<String, Object> delete(String id) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            adminDAO.delete(id);
            map.put("message","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败");
        }
        return map;
    }

    @Resource
    AdminMapper adminMapper;
    @DelCache
    @AddLog(value = "添加管理员")
    @Override
    public HashMap<String, Object> add(Admin admin) {
        admin.setId(UUID.randomUUID().toString());
        admin.setStatus("1");
        HashMap<String, Object> map = new HashMap<>();
        try {

            adminMapper.insertSelective(admin);
            map.put("message","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","添加失败");

        }
        return map;
    }

    @Override
    public Admin queryById(String id) {

        return adminMapper.selectByPrimaryKey(id);
    }
    @DelCache
    @AddLog(value = "修改管理员")
    @Override
    public HashMap<String, Object> update(Admin admin) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            adminMapper.updateByPrimaryKeySelective(admin);
            map.put("message","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","添加失败");
        }
        return map;
    }


}
